﻿
namespace Admin.NET.Application.Service;
/// <summary>
///策略模式
/// </summary>
public class ZhiChengDengJiDto
{

    public string zhiCheng { get; set; }

    public int dengJI { get; set; }
}

public abstract class ShenHePanDuan
{
    protected ShenHePanDuan Successor; // 指向下一个处理者的引用  

    // 处理请求的方法  
    public abstract void HandleRequest(CheckShenHeXinXiInput request);

    // 设置下一个处理者  
    public void SetSuccessor(ShenHePanDuan successor)
    {
        this.Successor = successor;
    }

}

/// <summary>
/// 状态判断
/// </summary>
public class zhuangTaiShenHePanDuan : ShenHePanDuan
{

    public override void HandleRequest(CheckShenHeXinXiInput request)
    {
        //如果在做上级医生审核 待上级审核--》待输血科审核（上级医生审核）
        if (request.muBiaoDanJuLeiXing == DanJuZhuangTai.待输血科审核)
        {
            if (!(request.danJuZhuangTai == DanJuZhuangTai.待上级审核 || request.danJuZhuangTai == DanJuZhuangTai.待科主任审核))
            {
                throw Oops.Oh("单据状态为" + request.danJuZhuangTai + "，无法完成上级医生/科主任审核");
            }
        }//如果在做输血科审核
        else if (request.muBiaoDanJuLeiXing == DanJuZhuangTai.已审核)
        {
            if (!(request.danJuZhuangTai == DanJuZhuangTai.待输血科审核))
            {
                throw Oops.Oh("单据状态为" + request.danJuZhuangTai + "，无法完成输血科审核");
            }
        }

        //如果下一个有对象就下一个
        if (Successor != null)
        {
            Successor.HandleRequest(request);
        }

    }
}

/// <summary>
/// 职称判断
/// </summary>
public class zhiChengPanDuan : ShenHePanDuan
{

    public override void HandleRequest(CheckShenHeXinXiInput request)
    {
        if (!request.muBiaoZhiCheng.IsNullOrEmpty())
        {
            List<ZhiChengDengJiDto> zhiChengList = new List<ZhiChengDengJiDto>()
       { new ZhiChengDengJiDto {zhiCheng=ZhiCheng.初级,dengJI=0 } , new ZhiChengDengJiDto {zhiCheng=ZhiCheng.中级,dengJI=1 }, new ZhiChengDengJiDto { zhiCheng = ZhiCheng.副高, dengJI = 2 }, new ZhiChengDengJiDto { zhiCheng = ZhiCheng.高级, dengJI = 3 } };

            if (zhiChengList.Where(a => a.zhiCheng == request.zhiCheng).Count() <= 0)
            {
                throw Oops.Oh("审核人职称未达到" + request.muBiaoZhiCheng + "，无法审核。如果存在职称信息维护错误，请联系运维工程师");
            }

            if (!request.muBiaoZhiCheng.IsNullOrEmpty())
            {
                var zhiChengDengji = zhiChengList.Where(a => a.zhiCheng == request.zhiCheng).Select(a => a.dengJI).FirstOrDefault();

                var muBiaoZhiChengDengji = zhiChengList.Where(a => a.zhiCheng == request.zhiCheng).Select(a => a.dengJI).FirstOrDefault();
                if (zhiChengDengji < muBiaoZhiChengDengji)
                {
                    throw Oops.Oh("审核人职称未达到" + request.muBiaoZhiCheng + "，无法审核。如果存在职称信息维护错误，请联系运维工程师");
                }
            }
        }


        //如果下一个有对象就下一个
        if (Successor != null)
        {
            Successor.HandleRequest(request);
        }

    }
}

/// <summary>
/// 权限判断
/// </summary>
public class quanXianShenHePanDuan : ShenHePanDuan
{

    public override void HandleRequest(CheckShenHeXinXiInput request)
    {
        var isHavePerm = request.permList.Where(a => a == request.quanXian).Count() > 0;
        if (!isHavePerm)
        {
            throw Oops.Oh("操作用户无权限");
        }

        //如果下一个有对象就下一个
        if (Successor != null)
        {
            Successor.HandleRequest(request);
        }
    }
}

/// <summary>
/// 相同科室判断
/// </summary>
public class xiangTongKeShiShenHePanDuan : ShenHePanDuan
{

    public override void HandleRequest(CheckShenHeXinXiInput request)
    {
        if (request.userKeShiIds == null || request.userKeShiIds.Count == 0) {

            throw Oops.Oh("用户未设置科室！如系统设置问题请联系运维人员！");
        }else {
            var isHaveKeShi = request.userKeShiIds.Where(a => a == request.keShiId).Count() > 0;
            if (isHaveKeShi)
            {
                throw Oops.Oh("用户科室,无此单据操作权限,无法操作！如系统设置问题请联系运维人员");
            }
        }



        //如果下一个有对象就下一个
        if (Successor != null)
        {
            Successor.HandleRequest(request);
        }

    }
}


